package com.fhz.day014;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

public class MainActivity2 extends AppCompatActivity {

    private MyBorder myBorder;
    private static final String TAG = "feng";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        //动态的注册一个广播接收者
        myBorder = new MyBorder();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.feng");
        registerReceiver(myBorder,intentFilter);




    }

    @Override
    protected void onResume() {
        super.onResume();
        //通过intentService 去下载一个json 然后通过广播发给这个面.----
        Intent intent = new Intent(this, MyIntentService2.class);
        startService(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myBorder);
    }

    class MyBorder extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle extras = intent.getExtras();
            String json = extras.getString("json");
            Log.i(TAG, "onReceive: "+json);
        }
    }
}